package com.telit.dispose.controller.dispose;

import com.telit.common.entity.DataReturn;
import com.telit.common.util.DateUtils;
import com.telit.dispose.dto.*;
import com.telit.dispose.entity.dispose.PlatMetaFieldEntity;
import com.telit.dispose.service.dispose.PlatMetaTableService;
import com.telit.dispose.vo.dispose.PlatTableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * Create by Chencf 2022/08/04
 */

@RestController
@RequestMapping("/api/table/")
@Api(tags = "desc = 元数据-表接口")
public class PlatMetaTableController {

    @Autowired
    private PlatMetaTableService platMetaTableService;


    @PostMapping("createTable")
    @ApiOperation("01--->创建物理表(资源表 同时关联资源分类表)Table")
    public DataReturn createTablePlatMetaTable(@RequestBody PlatMetaTableEntityDTO dto) {
        boolean b = platMetaTableService.createTablePlatMetaTable(dto);
        return DataReturn.ok();
    }


    @GetMapping("selectAllTableAndFiles")
    @ApiOperation("02--->查询使用的配置名称")
    public DataReturn selectAllTableAndFiles(TableFindDTO tableFindDTO) {
        Object o = platMetaTableService.selectAllTableAndFiles(tableFindDTO);
        return DataReturn.ok(o);
    }


    @GetMapping("selectTableAndFilesByTableId")
    @ApiOperation("03--->查询物理表已经对应的表字段")
    public DataReturn selectTableAndFilesByTableId(@RequestParam("tableId") String tableId) {
        List<PlatMetaFieldEntity> vo = platMetaTableService.selectTableAndFilesByTableId(tableId);
        return DataReturn.ok(vo);
    }


    @GetMapping("selectTableByTableId")
    @ApiOperation("04--->根据tableId查询对应表数据")
    public DataReturn<PlatMetaTableEntityDTO> selectTableByTableId(@RequestParam("tableId") String tableId) {
        PlatMetaTableEntityDTO vo = platMetaTableService.selectTableByTableId(tableId);
        return DataReturn.ok(vo);
    }

    @GetMapping("deleteTable")
    @ApiOperation("05--->删除设备配置（删除表）")
    public DataReturn deleteTable(@RequestParam(value = "tableId") String tableId) {
        platMetaTableService.deleteTable(tableId);
        return DataReturn.ok();
    }


    @PostMapping("updateOrDeleteTableFile/{flag}")
    @ApiOperation("06--->更新或者删除某一个表的字段  flag字段标识  flag=0 新增  flag=1 删除字段")
    public DataReturn updateOrDeleteTableFile(@PathVariable(value = "flag") Integer flag,
                                              @RequestBody PlatMetaTableEntityDTO dto) {
        Boolean vo = platMetaTableService.updateOrDeleteTableFile(flag, dto.getPlatMetaTableEntity());
        return DataReturn.ok(vo);
    }


    @PostMapping("updateIconShow")
    @ApiOperation("07--->修改--控制资源在图层显示接口")
    public DataReturn updateIconShow(@RequestBody PlatMetaTableUpdateDTO dto) {
        Boolean vo = platMetaTableService.updateIconShow(dto);
        return DataReturn.ok(vo);
    }


    @GetMapping("selectAllTable")
    @ApiOperation("08--->查询所有的的表")
    public DataReturn<List<PlatTableVO>> selectAllTable(@RequestParam(value = "flag", required = false) String flag) {
        List<PlatTableVO> vo = platMetaTableService.selectAllTable(flag);
        return DataReturn.ok(vo);
    }


    @GetMapping("selectTableInfoByTableName")
    @ApiOperation("09--->根据表查询表的内容")
    public DataReturn<List<Map>> selectTableInfoByTableName(@RequestParam(value = "tables") String tables,
                                                            @RequestParam(value = "districtCode", required = false) String districtCode)
            throws Exception {
        List<Map> vo = platMetaTableService.selectTableInfoByTableName(tables, districtCode);
        return DataReturn.ok(vo);
    }


    @ApiOperation("10--->资源数据导出--通用接口")
    @GetMapping("commExportResource")
    public void commExportResource(@RequestParam(value = "tableId") String tableId,
                                   @RequestParam(value = "tableName") String tableName,
                                   @RequestParam(value = "resourceName") String resourceName,
                                   @RequestParam(value = "xzqhcode", required = false) String xzqhcode,
                                   @RequestParam(value = "name", required = false) String name,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        platMetaTableService.commExportResource(tableId, tableName, resourceName, request, response, xzqhcode, name);
    }

    @ApiOperation("11--->资源数据导入--通用模板下载")
    @GetMapping("commImportTemplate")
    public void commImportTemplate(@RequestParam(value = "tableId") String tableId,
                                   @RequestParam(value = "tableName") String tableName,
                                   @RequestParam(value = "resourceName") String resourceName,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        platMetaTableService.commImportTemplate(tableId, tableName, resourceName, request, response);
    }


    @ApiOperation(value = "12--->资源数据导入--导入数据")
    @PostMapping(value = "commLeadData/{tableId}/{tableName}")
    public void commLeadData(@PathVariable(value = "tableId") String tableId,
                             @PathVariable(value = "tableName") String tableName,
                             MultipartFile file) {

        platMetaTableService.commLeadData(tableId, tableName, file);

    }

    @ApiOperation(value = "13--->调整资源显示或者不显示")
    @PostMapping(value = "updateResourceShow")
    public DataReturn updateResourceShow(@RequestBody UpdateResourceShowDTO updateResourceShow) {

        platMetaTableService.updateResourceShow(updateResourceShow);
        return DataReturn.ok();
    }


    @ApiOperation(value = "14--->修改某一个资源类型，分类 图标url等")
    @PostMapping(value = "updateResourceCategory")
    public DataReturn updateResourceCategory(@RequestBody UpdateResourceCategoryDTO updateResourceCategoryDTO) {

        platMetaTableService.updateResourceCategory(updateResourceCategoryDTO);
        return DataReturn.ok();
    }



    @ApiOperation(value = "15--->新增资源分类")
    @PostMapping(value = "addResourceCategory")
    public DataReturn addResourceCategory(@RequestBody AddResourceCategoryDTO addResourceCategoryDTO) {
        platMetaTableService.addResourceCategory(addResourceCategoryDTO);
        return DataReturn.ok();
    }


    public static void main(String[] args) {

        boolean matches = "2022-11-11".matches(DateUtils.DATE_STR_PATTERN.pattern());
        System.out.println("matches = " + matches);
    }

}
